package com.awspaas.user.apps.cpq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.cpq.util.RefreshDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshModulDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshTempDataUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class CPQ_sendMsgToRabbitMQWeb {
	private final static String QUEUE_NAME = "test_queue";
	public String sendMsgToRabbitMQ(UserContext userContext,String bindId, String rowData) throws Exception {
		try {
//			   // 1. 创建连接工厂
//	        ConnectionFactory factory = new ConnectionFactory();
//	        factory.setHost("localhost");
//	         factory.setUsername("guest");
//	        factory.setPassword("guest");
//	        try (Connection connection = factory.newConnection();
//	                Channel channel = connection.createChannel()) {
//	               channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//	               
//	               // 发送10条测试消息
//	                   String message = rowData;
//	                   channel.basicPublish("", QUEUE_NAME, 
//	                       MessageProperties.PERSISTENT_TEXT_PLAIN,
//	                       message.getBytes("UTF-8"));
//	                   System.out.println(" [x] Sent '" + message + "'");	               
//	           }
			
			 JSONArray dataList = JSONObject.parseArray(rowData);
             JSONObject jsonObject1 = (JSONObject) dataList.get(0);
             bindId=jsonObject1.getString("bindId");
             String bms = "";
 			// 获取需要更新的物料编号。
 			Map<String, JSONObject> userMap = new HashMap<String, JSONObject>();
 			// 用于存放更新后的装配件信息
 			Map<String, JSONObject> zpjMap = new HashMap<String, JSONObject>();
 			List<String> bhList = new ArrayList<>();
 			for (int i = 0; i < dataList.size(); i++) {
 				JSONObject jsonObject = (JSONObject) dataList.get(i);
 				String bm = (String) jsonObject.get("BM");
 				String status = (String) jsonObject.get("STATUS");
 				String type = (String) jsonObject.get("TYPE");
 				if (status.equals("启用")) {
 					JSONObject json = new JSONObject();
 					json.put("DJ", (String) jsonObject.get("DJ"));
 					json.put("CLF", (String) jsonObject.get("CLF"));
 					json.put("RGF", (String) jsonObject.get("RGF"));
 					json.put("WXWG", (String) jsonObject.get("WXWG"));
 					json.put("CGF", (String) jsonObject.get("CGF"));
 					json.put("ZL", (String) jsonObject.get("ZL"));
 					json.put("TJ", (String) jsonObject.get("TJ"));
 					json.put("NAME", (String) jsonObject.get("NAME"));
 					json.put("GG", (String) jsonObject.get("GG"));
 					json.put("TH", (String) jsonObject.get("TH"));
 					json.put("DW", (String) jsonObject.get("DW"));
 					json.put("TYPE", (String) jsonObject.get("TYPE"));// 类型
 					json.put("BM", bm);
 					bhList.add(bm);
 					if(type.equals("装配件")) {
 						json.put("ZJS", "");
 						zpjMap.put(bm, json);
 					}else {
 						userMap.put(bm, json);
 					}
 					
 					if (bms.isEmpty()) {
 						bms = "'" + bms + "'";
 					} else {
 						bms += ",'" + bms + "'";
 					}
 				}

 			}
 			/**
 			 * 1、获取所有状态为启用的类型为装配件物料信息BO_EU_CPQ_COMPONENT_PART
 			 */

 			String allCPsql = "select   a.BM,a.ID,a.LX,a.SL,a.ZL,a.TJ,a.CLF,a.RGF,a.WXWG,a.CGF,a.DJ,a.ZJ,a.ZCLF, "
 					+ "a.ZGSF,a.ZWX,a.ZCGF,a.ZZL,a.ZTJ,"
 					+ "b.NAME,b.GG,b.TH,b.DW,b.BM as BH,b.TYPE,b.ZL as WZL,b.TJ as WTJ,b.DJ as JG,b.CLF as WCLF,b.RGF as WRGF,b.WXWG as WX,b.CGF as WCGF "
 					+ "from  BO_EU_CPQ_COMPONENT_PART_GRID a, BO_EU_CPQ_COMPONENT_PART b "
 					+ "where  a.BINDID=b.BINDID and  b.TYPE='装配件' ";
 			List<RowMap> allmaps = DBSql.getMaps(allCPsql);
 			// 存放需要更新的装配件信息
 			Map<String, List<RowMap>> aboutCPMap = new HashMap<>();
 			if (allmaps.size() > 0) {
 				for (RowMap rowMap : allmaps) {
 					String bh = rowMap.getString("BH");
 					if (aboutCPMap.containsKey(bh)) {
 						List<RowMap> lists = aboutCPMap.get(bh);
 						lists.add(rowMap);
 						aboutCPMap.put(bh, lists);
 					} else {
 						List<RowMap> lists = new ArrayList<RowMap>();
 						lists.add(rowMap);
 						aboutCPMap.put(bh, lists);
 					}

 				}
 			}
 			// 将需要更新的装配件进行价格更新
 			String zpjsql = "update BO_EU_CPQ_COMPONENT_PART set DJ=?,CLF=?,RGF=?,WXWG=?,CGF=?,ZL=?,TJ=? where BM=?";
 			String zjsql = "update BO_EU_CPQ_COMPONENT_PART_GRID set DJ=?,CLF=?,RGF=?,WXWG=?,CGF=?,ZL=?,TJ=?,ZJ=?,"
 					+ "ZCLF=?,ZGSF=?,ZWX=?,ZCGF=?,ZZL=?,ZTJ=?,MC=?,LX=?,GG=?,DH=?,DW=? where BM=? and  ID=?";
 			// 将勾选需要更新的物料信息中的装配件进行单价更新，判断装配件中的子项是否包含勾选的物料信息中的采购件和制造件，如果包含则需要将装配件的单价进行更新后，再进行下一步操作
 			/**
 			 * 将更新后的勾选数据进行装配件的更新，更新完成后将装配件存放在map中，在递归将所有需要更新的装配件进行更新，将所有更新的装配件信息存放在map中。
 			 */

 			// 获取更新后的装配件信息
 			Map<String, JSONObject> zpjNewMap = RefreshDataUtils.getZPTMaterials(aboutCPMap, zpjMap, userMap, zpjsql,
 					zjsql);

 			/**
 			 * 将更新后的新的装配件，进行递归更新。将更新后的数据存放到map中
 			 * 
 			 */
 			Map<String, JSONObject> map = RefreshDataUtils.getZPJMsgs(zpjNewMap, aboutCPMap, zpjsql, zjsql);
 			/**
 			 * 2、获取所有状态为启用的模块信息BO_EU_CPQ_MODULE_MSG
 			 */
 			// 将更新完成后的数据，加上勾选的数据作为参数
 			for (String key : userMap.keySet()) {
 				map.put(key, userMap.get(key));
 			}
 			Map<String, Map<String, Object>> modulLists = RefreshModulDataUtils.getModule(map);
 			/**
 			 * 3、获取所有状态为启用的项目报价标准模板信息BO_EU_CPQ_TEMPLATE_PRICE
 			 */
 			RefreshTempDataUtils.upateTemp(modulLists);
             
             
             
            
 			SDK.getBOAPI().updateByBindId("BO_EU_CPQ_COMPONENT_PART", bindId,"TS", "可编辑"); 
	    	ResponseObject ro = ResponseObject.newOkResponse();
			return ro.toString();
		} catch (Exception e) {
			SDK.getBOAPI().updateByBindId("BO_EU_CPQ_COMPONENT_PART", bindId,"TS", "可编辑"); 
			throw e;
		}
		
	}

}
